/*

This file is part of roomyak.

Copyright (C) 2008 Pixecur Technologies Incorporated (Esotera).
Visit Esotera at http://www.esoteras3.com
 
roomyak is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
 
roomyak is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with roomyak; see the file COPYING.  If not, write to 
Pixecur Technologies, 213-31 Peet Street, St. John's, NL, A1B 3W8. 
 
Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
 
*/

package com.datasentinel.util.thumbnail;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.filechooser.FileSystemView;

import sun.awt.shell.ShellFolder;

/**
 * @author rickp
 * 
 */
public class DefaultThumbnailProvider implements ThumbnailProvider
{
	@Override
	public Image getSmallThumbnail(File file) throws ThumbnailException
	{
		return getThumbnail(file, false);
	}

	@Override
	public Image getThumbnail(File file) throws ThumbnailException
	{
		return getThumbnail(file, true);
	}
	
	/*
	 * throw IOException if image cannot be loaded
	 */
	protected BufferedImage getImage(File imageFile) throws IOException
	{
		return ImageIO.read(imageFile);
	}

	private Image getThumbnail(File file, boolean large)
			throws ThumbnailException
	{
		Image image = null;
		try
		{
			BufferedImage imageTmp = getImage(file);
			if (imageTmp != null)
			{
				imageTmp = resize(imageTmp, imageTmp.getWidth(), imageTmp
						.getHeight());
				float imageTmpWidth = imageTmp.getWidth();
				float imageTmpHeight = imageTmp.getHeight();
				double maxScaledDim = 384.0;
				double maxImageDim = Math.max(imageTmpWidth, imageTmpHeight);
				if (maxImageDim > maxScaledDim)
				{
					double factor = maxScaledDim / maxImageDim;
					int scaledWidth = (int)Math.ceil(imageTmpWidth * factor);
					int scaledHeight = (int)Math.ceil(imageTmpHeight * factor);
					imageTmp = resize(imageTmp, scaledWidth, scaledHeight);
				}
				image = imageTmp;
			}
			else throw new IOException("file ["+file+"] is probably not image");
		}
		catch (IOException ioe)
		{
			try
			{
				ShellFolder sf = ShellFolder.getShellFolder(file);
				image = sf.getIcon(large);
				if (!(image instanceof Image))
				{
					ImageIcon icon = (ImageIcon)FileSystemView
							.getFileSystemView().getSystemIcon(file);
					image = icon.getImage();
				}
			}
			catch (Exception e)
			{
				throw new ThumbnailException(e);
			}
		}
		return image;
	}

	private BufferedImage resize(BufferedImage image, int width, int height)
	{
		BufferedImage resizedImage = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_ARGB);
		Graphics2D g = resizedImage.createGraphics();
		g.drawImage(image, 0, 0, width, height, null);
		g.dispose();
		return resizedImage;
	}
}
